package com.aaron.leetcode.lc03;

import java.util.HashMap;

class Solution03_2 {

    //滑动窗口解法

    public int lengthOfLongestSubstring(String s) {
        HashMap<Character, Integer> window = new HashMap<>();
        int left = 0, right = 0;

        int res = 0;

        while (right < s.length()) {
            char c = s.charAt(right);
            right++;
            window.compute(c, (k, v) -> null == v ? 1 : v + 1);
            while (null != window.get(c) && window.get(c) > 1) {
                char d = s.charAt(left);
                left++;
                window.compute(d, (k, v) -> v - 1);
            }
            res = Math.max(res, right - left);
        }
        return res;
    }

    public static void main(String[] args) {
        Solution03_2 solution03_2 = new Solution03_2();
//        String s = "11223455";
        String s = "11111233asdfg";
        int ans = solution03_2.lengthOfLongestSubstring(s);
        System.out.println(ans);
    }

}